﻿/*
 * Created by SharpDevelop.
 * User: Jack Brinegar
 * Date: 1/28/2013
 * Time: 8:01 AM
 * 
 * To change this template use Tools | Options | Coding | Edit Standard Headers.
 */
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows;
using System.Windows.Forms;
using System.Configuration;
using System.Xml.Serialization;
using System.IO;

namespace AdminDesktop
{
	/// <summary>
	/// Description of MainForm.
	/// </summary>
	public partial class MainForm : Form
	{
		public ButternutSkiPatrol.DataFile dataFile = null;
		
		public AdminDesktop.Controls.ucRoster pageRoster; //= new AdminDesktop.Controls.ucRoster();
		public AdminDesktop.Controls.ucServiceDays pageServiceDays; //= new AdminDesktop.Controls.ucServiceDays();
		public AdminDesktop.Controls.ucReportViewer pageReport;
		
		private bool configLoaded = false;
		private bool rosterLoaded = false;
		
		public int StatusBarHeight
		{
			get { return this.statusMain.Height;}
		}
		
		public int MenuBarHeight
		{
			get { return this.menuMain.Height;}
		}
        
		public void ForceRosterRefresh()
		{
			pageRoster.LoadData();
			pageServiceDays.LoadData();
		}
		
		public MainForm()
		{
			//
			// The InitializeComponent() call is required for Windows Forms designer support.
			//
			InitializeComponent();

			LoadAppConfig();
			
			if(configLoaded)
			{
				#region setup the menu
				for(int i = 0; i<DataEngine.APP_CONFIG.Reports.ReportList.Count;i++)
				{
					ToolStripItem mi = reportsToolStripMenuItem.DropDownItems.Add(DataEngine.APP_CONFIG.Reports.ReportList[i].Name);
					mi.Tag = DataEngine.APP_CONFIG.Reports.ReportList[i].File;
					mi.Click += new EventHandler(mi_Click);
				}
				#endregion
			
				LoadData();
			}
			
			if(rosterLoaded)
			{				
				this.Text = "Butternut Ski Patrol Adminitrative Desktop";
				this.reportsToolStripMenuItem.Enabled=true;
				
				
				//controls
				pageRoster = new AdminDesktop.Controls.ucRoster();
				this.splitContainer1.Panel2.Controls.Add(pageRoster);
				pageRoster.Visible=false;
				
				pageServiceDays= new AdminDesktop.Controls.ucServiceDays();
				this.splitContainer1.Panel2.Controls.Add(pageServiceDays);
				pageServiceDays.Visible=false;
				
				pageReport = new AdminDesktop.Controls.ucReportViewer();
				this.splitContainer1.Panel2.Controls.Add(pageReport);
				pageReport.Visible=false;
				this.reportsToolStripMenuItem.Enabled=true;
				pageRoster.Dock= DockStyle.Fill;
			}
			else
			{
				this.Text = "Butternut Ski Patrol Adminitrative Desktop";
				this.reportsToolStripMenuItem.Enabled=false;
			}
		}

		
		
		private void LoadAppConfig()
		{
			try
			{
				DataEngine.LoadAppConfig();
				CheckDirectories();
				configLoaded = true;
			}
			catch (Exception ex)
			{
				configLoaded = false;
			}
		}
		
		private void CheckDirectories()
		{
			//create any directories needed by the system	
			if(!System.IO.Directory.Exists(DataEngine.APP_CONFIG.DataDirectory)) {System.IO.Directory.CreateDirectory(DataEngine.APP_CONFIG.DataDirectory);}
			if(!System.IO.Directory.Exists(DataEngine.APP_CONFIG.BackupDataDirectory)) {System.IO.Directory.CreateDirectory(DataEngine.APP_CONFIG.BackupDataDirectory);}
			if(!System.IO.Directory.Exists(DataEngine.APP_CONFIG.TemporaryDirectory)) {System.IO.Directory.CreateDirectory(DataEngine.APP_CONFIG.TemporaryDirectory);}
		}
		
		public void ShowStatus(string Message)
		{
			this.Status.Text = Message;
		}
		
		private void LoadData()
		{
			try
			{
				//load the last used file...
				if((DataEngine.APP_CONFIG.LastRosterFile != null)&&(DataEngine.APP_CONFIG.LastRosterFile != string.Empty))
				{
					ShowStatus(AdminDesktop.DataEngine.LoadRosterFile(DataEngine.APP_CONFIG.LastRosterFile));
					rosterLoaded=true;
				}
				else
				{
					//need to create a new Roster File
					this.MnuOpenDatafileClick(this,EventArgs.Empty);
					//this.MnuNewDatafileClick(this,EventArgs.Empty);
				}
				dataFile = AdminDesktop.DataEngine.dataFile;
				//rosterLoaded = true;
				
			}
			catch (Exception ex)
			{
				rosterLoaded = false;
			}
			
			
		}
		
		void ExitToolStripMenuItemClick(object sender, EventArgs e)
		{
			Application.Exit();
		}
			
		void SplitContainer1SplitterMoved(object sender, SplitterEventArgs e)
		{
			//hardcoded for now...until I work out the configuration classes
			if(splitContainer1.SplitterDistance>100)
			{
				splitContainer1.SplitterDistance=100;
			}
			else if(splitContainer1.SplitterDistance<50)
			{
				splitContainer1.SplitterDistance=50;
			}
		}
		
		void BtnRosterClick(object sender, EventArgs e)
		{
			pageRoster.Visible=true;
			pageServiceDays.Visible=false;
			pageReport.Visible= false;
			
			this.splitContainer1.Panel2.Dock = DockStyle.Fill;
			this.splitContainer1.Panel2.Anchor = AnchorStyles.Left;
			//this.splitContainer1.Panel2.Controls[0].Width = this.splitContainer1.Panel2.Width;
			//this.splitContainer1.Panel2.Controls[0].Height = this.splitContainer1.Panel2.Height-this.statusMain.Height;
			//this.splitContainer1.Panel2.Refresh();
		}
		
		void BtnServiceDaysClick(object sender, EventArgs e)
		{
			pageRoster.Visible=false;
			pageServiceDays.Visible=true;
			pageReport.Visible=false;
		}
		
//		void ServiceDayReportToolStripMenuItemClick(object sender, EventArgs e)
//		{
//			pageRoster.Visible=false;
//			pageServiceDays.Visible=false;
//			pageReport.Visible=true;
//			pageReport.LoadReport(@"C:\Projects\ButternutSkiPatrol\AdminDesktop\Reports\ServiceDays.xsl");
//		}
		
		void MnuOpenDatafileClick(object sender, EventArgs e)
		{
			this.openFileDialog.Title="Open Datafile...";
			this.openFileDialog.Filter="Desktop Datafile (*.ddf)|*.ddf";
			this.openFileDialog.DefaultExt="*.ddf";
			this.openFileDialog.Multiselect=false;
			this.openFileDialog.InitialDirectory=@"C:\Projects\ButternutSkiPatrol\AdminDesktop\Data";
			DialogResult result = this.openFileDialog.ShowDialog();
			if(result==DialogResult.OK)
			{
				ShowStatus(AdminDesktop.DataEngine.LoadRosterFile(this.openFileDialog.FileName));
				
				rosterLoaded=true;
				if(rosterLoaded)
				{				
					//controls
					pageRoster = new AdminDesktop.Controls.ucRoster();
					this.splitContainer1.Panel2.Controls.Add(pageRoster);
					pageRoster.Visible=false;
					
					pageServiceDays= new AdminDesktop.Controls.ucServiceDays();
					this.splitContainer1.Panel2.Controls.Add(pageServiceDays);
					pageServiceDays.Visible=false;
					
					pageReport = new AdminDesktop.Controls.ucReportViewer();
					this.splitContainer1.Panel2.Controls.Add(pageReport);
					pageReport.Visible=false;
					this.reportsToolStripMenuItem.Enabled=true;
					this.Text = Application.ProductName + " [" + DataEngine.DataFileName +"]";
				}
				else
				{
					this.Text = "Butternut Ski Patrol Adminitrative Desktop";
					this.reportsToolStripMenuItem.Enabled=false;
				}
			
				//datafile = AdminDesktop.DataEngine.dataFile;
				this.pageServiceDays.LoadData();
				this.pageRoster.LoadData();
				DataEngine.APP_CONFIG.LastRosterFile=this.openFileDialog.FileName;
				DataEngine.SaveAppConfig();
			}
		}
		
		void MnuNewDatafileClick(object sender, EventArgs e)
		{
			this.saveFileDialog.Title = "Create a new Roster...";
			this.saveFileDialog.Filter="Desktop Datafile (*.ddf)|*.ddf";
			this.saveFileDialog.DefaultExt="*.ddf";
			
			//this.openFileDialog.Multiselect=false;
			this.saveFileDialog.InitialDirectory=@"C:\Projects\ButternutSkiPatrol\AdminDesktop\Data";
			DialogResult result = this.saveFileDialog.ShowDialog();
			if(result==DialogResult.OK)
			{
				AdminDesktop.DataEngine.CreateNewRosterFile(this.saveFileDialog.FileName);
				this.Text = Application.ProductName + " [" + DataEngine.DataFileName +"]";
			}
			if(result== DialogResult.Cancel)
			{
				rosterLoaded=false;
			}
		}
		
		void OptionsToolStripMenuItemClick(object sender, EventArgs e)
		{
			frmOptions options = new frmOptions();
			options.Show(this);
		}
		
		void mi_Click(object sender, EventArgs e)
		{
			pageRoster.Visible=false;
			pageServiceDays.Visible=false;
			pageReport.Visible=true;
			
			ToolStripItem mi = (ToolStripItem)sender;
			pageReport.LoadReport(mi.Tag.ToString());
		}
	}
}
